package com.zkdcloud.controller.mobile;

import com.xdja.interceptor.bean.ResultBean;
import com.zkdcloud.dto.UserBean;
import com.zkdcloud.service.UserService;
import com.zkdcloud.util.ResponseAndroidUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;

import static com.zkdcloud.constant.Constants.HTTP_CONSTANTS.OPERATOR_SUCCESS;

/**
 * 注册controller
 *
 * @author zk
 * @since 2018/4/10
 */
@RestController
@RequestMapping(value = "/api/register")
public class MobileRegisterController {
    /**
     * static logger
     */
    private static Logger logger = LoggerFactory.getLogger(MobileRegisterController.class);

    @Autowired
    private UserService userService;
    /**
     * 发送手机号码
     *
     * @return resultBean
     */
    @RequestMapping(value = "/sendPhone",method = RequestMethod.POST)
    public ResultBean sendPhoneNumber(UserBean user, HttpServletResponse response){
        try {
            userService.sendPhoto(user.getMobile());
        } catch (Exception e) {
            ResponseAndroidUtil.responseError(e.getMessage(),response);
        }
        return ResultBean.successResult(OPERATOR_SUCCESS);
    }
    /**
     * 验证验证码
     *
     * @return resultBean
     */
    @RequestMapping(value = "/verifyCode",method = RequestMethod.POST)
    public ResultBean verifyPhoneNumber(String mobile,String code, HttpServletResponse response){
        try {
            userService.verifyMobileCode(mobile,code);
        } catch (Exception e) {
            ResponseAndroidUtil.responseError(e.getMessage(),response);
        }
        return ResultBean.successResult(OPERATOR_SUCCESS);
    }

    /**
     * 发送注册请求
     *
     * @param user user
     * @return resultBean
     */
    @RequestMapping(value = "/add",method = RequestMethod.POST)
    public ResultBean register(UserBean user){
        userService.addUser(user);
        return ResultBean.successResult(OPERATOR_SUCCESS);
    }
}
